// This is the main DLL file.

#include "stdafx.h"

#include "GUI.h"

GUI::GUI() {
	GUI::initializeCEGUI();
}

void GUI::initializeCEGUI() {
	// Initialize CEGUI
	GUI::mRenderer = &CEGUI::OgreRenderer::bootstrapSystem();
	
	// Now that CEGUI has been initialized, we need to set the so-called default resource groups for each of CEGUI'S resource managers. 
	CEGUI::Imageset::setDefaultResourceGroup("Imagesets");
	CEGUI::Font::setDefaultResourceGroup("Fonts");
	CEGUI::Scheme::setDefaultResourceGroup("Schemes");
	CEGUI::WidgetLookManager::setDefaultResourceGroup("LookNFeel");
	CEGUI::WindowManager::setDefaultResourceGroup("Layouts");

	// The following line of code selects the skin:
	CEGUI::SchemeManager::getSingleton().create("TaharezLook.scheme");

	// The next thing we need to do is set the default mouse cursor:
	CEGUI::System::getSingleton().setDefaultMouseCursor("TaharezLook", "MouseArrow");

}

void GUI::updateGUI(const Ogre::FrameEvent& evt) {
	CEGUI::System::getSingleton().injectTimePulse(evt.timeSinceLastFrame);
}

void GUI::setGUIsheet(string layoutName) {
	// Try TextDemo
	CEGUI::Window *guiRoot = CEGUI::WindowManager::getSingleton().loadWindowLayout(layoutName +".layout"); 
	CEGUI::System::getSingleton().setGUISheet(guiRoot);
}

void GUI::keyDownGUI(const OIS::KeyEvent &arg) {
	CEGUI::System &sys = CEGUI::System::getSingleton();
	sys.injectKeyDown(arg.key);
	sys.injectChar(arg.text);
}

void GUI::keyUpGUI(const OIS::KeyEvent &arg) {
	CEGUI::System::getSingleton().injectKeyUp(arg.key);
}

void GUI::mouseMovedGUI(const OIS::MouseEvent &arg) {
	CEGUI::System &sys = CEGUI::System::getSingleton();
	sys.injectMouseMove(arg.state.X.rel, arg.state.Y.rel);
	// Scroll wheel.
	if (arg.state.Z.rel)
    sys.injectMouseWheelChange(arg.state.Z.rel / 120.0f);
}

void GUI::mouseDownGUI(const OIS::MouseButtonID id) {
	CEGUI::System::getSingleton().injectMouseButtonDown(GUI::convertButton(id));
}

void GUI::mouseUpGUI(const OIS::MouseButtonID id) {
	CEGUI::System::getSingleton().injectMouseButtonUp(GUI::convertButton(id));
}

CEGUI::MouseButton GUI::convertButton(OIS::MouseButtonID buttonID) {
	switch (buttonID)
    {
    case OIS::MB_Left:
        return CEGUI::LeftButton;
 
    case OIS::MB_Right:
        return CEGUI::RightButton;
 
    case OIS::MB_Middle:
        return CEGUI::MiddleButton;
 
    default:
        return CEGUI::LeftButton;
    }
}